<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Server\QueuePushService as QPS;

class CommonController extends BaseController {
    
        private $Zhongtong_Url = 'http://58.40.16.125:9001/zto/api_utf8/kdService';

	/**
	 * @desc: 发送手机短信验证码
	 * @Author: delayhsiao
     * @again quyang 2017-1-17
	 * @update: 2016-11-24
	 **/
	public function sendVerifyCode(){
		$data = I('param.');
		if(empty($data['mobile'])){
			$this->output(false);
		}
		$repeat = D('Home/User')->checkMobileRepeat($data['mobile']);
		if(!$repeat){
			$this->auto_error('手机号已存在');
		}
		$verifyCode = rand(100000,999999);
		$smsTpl = C('AUTOGRAPH')."您的验证码为：".$verifyCode."，如有任何疑问可联系在线客服或拨打025-84185858";
		$result = send_sms($data['mobile'],$smsTpl);
		$errCode = explode("\n",$result);
		$err = explode(',',$errCode[0]);
		if($err[1] == 0){
			$return = array('verifyCode' => (string)$verifyCode);
			$this->output(true,$return);
		}else{
			$this->output(false);
		}
	}

	/**
	 * @desc 获取首页banner接口
	 * @Author quyang
	 * @update 2016-11-23
	 */
	public function queryBanner(){
	    $data = I('param.');
	    if(empty($data['sysCode'])){
	        $this->param_error();
	    }
	    $where = array('f_sys_code'=>$data['sysCode']);
	    $banner = D('Home/Banner')->banner($where);
	    if ($banner == false){
	        $this->output(0);
	    }
	    $arr = array_values($banner);
	    $this->output(1,$arr);
	}

	/**
	 * @desc: 获取客户端版本
	 * @Author: delayhsiao
	 * @update: 2016-11-28
	 **/
	public function queryLatestVersionInfo(){
		$data = I('param.');
		if($data['deviceType'] =='' || empty($data['channel'])){
			$this->param_error();
		}

        $AppVersion = new \Home\Model\AppVersionLogModel();
        $version = $AppVersion->queryUpdateApp( $data['deviceType'] , $data['channel'] , '1', $this ->getPlatform_Data() );

		if(empty($version)){
			$this->output(true);
		}else{
			$this->output(true,$version['0']);
		}
	}

    /**
     * @Desc  : 客户端版本升级列表
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/23 13:46
     * @Update：2017/3/23
     **/
    public function queryLatestVersionList()
    {

        $data = I('param.');

        if ($data['bmxSource'] == 'pda')
        {
            $AppVersion = new \Home\Model\AppVersionLogModel();

            $version_formal = $AppVersion->queryUpdateApp( 0 , 'default' , '1' , '2',2);
            $version_experience = $AppVersion->queryUpdateApp( 0 , 'default' , '2' , '2');

            $version_stable_len=count($version_formal);
            for ($i=0;$i<$version_stable_len;$i++){
                if($version_formal[$i]['f_status']==0 && $version_formal[$i]['f_release_time']<date('YmdHis')){
                    M('tp_app_version_log')->where(['f_status'=>1,'f_update_type'=>$data['deviceType']])->save(['f_status'=>2]);
                    M('tp_app_version_log')->where(['f_status'=>0,'f_update_type'=>$data['deviceType']])->save(['f_status'=>1]);
                }
            }

            $version_formal = $AppVersion->queryUpdateApp( 0 , 'default' , '1' , '2',1);

            $version['formal'] = $version_formal['0'];  //pda 稳定版
            $version['experience'] = $version_experience['0'];  //pda 稳定版

            if(empty($version)){
                $this->output(true);
            }else{

                //没有数据则为空对象
                if ( $version['formal'] == '' )
                {
                    $version['formal'] = (object)[];
                }

                if ( $version['experience'] == '' )
                {
                    $version['experience'] = (object)[];
                }

                $this->output(true,$version);
            }
        }
        else
        {

            if($data['deviceType'] =='' || empty($data['channel'])){
                $this->param_error();
            }
            $AppVersion = new \Home\Model\AppVersionLogModel();

            //分别获取体验版和稳定版  1.稳定版  2.体验版
            $version_stable = $AppVersion->queryUpdateApp( $data['deviceType'] , $data['channel'] , '1' , '3',2);
            $version_experience = $AppVersion->queryUpdateApp( $data['deviceType'] , $data['channel'] , '2', '3' );
//            print_r($version_stable);exit();
            $version_stable_len=count($version_stable);
            for ($i=0;$i<$version_stable_len;$i++){
                if($version_stable[$i]['f_status']==0 && $version_stable[$i]['f_release_time']<date('YmdHis')){
                    M('tp_app_version_log')->where(['f_status'=>1,'f_update_type'=>$data['deviceType']])->save(['f_status'=>2]);
                    M('tp_app_version_log')->where(['f_status'=>0,'f_update_type'=>$data['deviceType']])->save(['f_status'=>1]);
                }
            }
            $version_stable = $AppVersion->queryUpdateApp( $data['deviceType'] , $data['channel'] , '1' , '3',1);
            $version['formal'] = $version_stable['0'];  //正式版
            $version['experience'] = $version_experience['0'];  //体验版

            if(empty($version)){
                $this->output(true);
            }else{

                //没有数据则为空对象
                if ( $version['formal'] == '' )
                {
                    $version['formal'] = (object)[];
                }

                if ( $version['experience'] == '' )
                {
                    $version['experience'] = (object)[];
                }

                $this->output(true,$version);
            }
        }


	}

    /**
     * Function getPlatform_Data
     *
     * Author: 黄盖
     * Date: 2017-4-11 17:35
     * Info: 获取哪种平台  0普通版 1专业版
     */
	public function getPlatform_Data()
    {
        $data = I('param.');

        return intval( $data['professional'] );
    }

	/**
	 * @desc 获取省市信息
	 * @author quyang
	 * @update 2016-11-28
	 */
	public function queryCity(){
	    $pid = I('param.pid');
	    if ($pid == ''){
	        $pid = '0';
	    }
	    $where = array('f_parentid'=>$pid);
	    $data = D('Home/Area')->getArea($where);
	    if ($data == false){
	        $this->output(0,$data);
	    }
	    $this->output(1,$data);
	}

	/**
	 * @desc:  帮助中心/关于我们 获取静态页面
	 * @Author: zhoutai
	 * @update: 16/11/28
	 **/

	public function staticPage(){
		$data = I('param.');
		if ($data['fType'] == '' || $data['source'] == '' || $data['fSysCode'] == '') $this->param_error();
		if ($data['source'] != '0' && $data['source'] != '1') $this->param_error();
		$where = array(
			'f_type' => $data['fType'],
			'f_sys_code' => $data['fSysCode']
		);
		if ($data['source'] == '0')
			$where['f_client'] = 0;
		else
			$where['f_PC'] = 0;
		$result = D('tp_static_page')->where($where)->field('
            f_content as fContent,
            f_title as fTitle
        ')->find();
		if (empty($result)) $this->output(0);
		$this->output(1, $result);
	}

	/**
	 * @desc: 获取支付套餐列表
	 * @Author: delayhsiao
	 * @update: 2016-11-29
	 **/
	public function queryPaySetMeal(){
	    $param = I('param.');
	    if ( !empty($param['userId']) ){
            $Order = new \Home\Model\OrderProductModel();
            if ( $Order->queryProductStatus( $param['userId'] ) > 0 ){
                $this->output(true,D('Home/Product')->queryPaySetMeal( $param['userId'] ));
            }
        }
		$setmeal = D('Home/Product')->queryPaySetMeal();
		$this->output(true,$setmeal);
	}
	
	/**
	 * @desc 查询微信发送状况
	 * @author quyang
	 * @update 2016-12-2
	 */
	public function queryWeinxin(){

	    //获取微信发送状况
        $noticeServer = new \Home\Server\ParcelNoticeResultServer();
        $data = $noticeServer->queryWeixinSeng();

	    if($data != false){

	        $speed = $list = [];
	        foreach ( $data as $v ){

	            if ( $v['f_take_code'] == 0 )
                {
                    $speed[] = $v['f_parcel_id'];
                }
                else
                {
                    $list[] = $v['f_parcel_id'];
                }

            }

//            $parcelId = substr($list,0,strlen($list)-1);
            //普通版包裹发送通知
			echo "CVN: ".count($list)."<br />";
			echo "SVN: ".count($speed)."<br />";

			//监控通知
			if(count($list) > C('TASK_WARNING') || count($speed) > C('TASK_WARNING') ){
				$con = C('AUTOGRAPH')."短信通知任务数量过大，请注意处理：CVN:".count($list).',SVN:'.count($speed);
				send_sms(C('NOTICE_MOBILE'),$con);
			}
			
			$this->queueSendSms( $list );

            //极速版通知
            $SpeedServer = new SpeedController();

            $SpeedServer->speedSendSms( $speed );

	    }
	}
	
	/**
	 * @desc 短信发送方法
	 * @author quyang
     * @again quyang 2017-1-17
	 * @update 2016-11-29
	 */
	public function repeatSendSms($parcelId,$spareNum=0/*备用接口发送*/){

        $parcelDetail = new \Home\Server\QueryParcelService();
        $parcelNotice = new \Home\Server\ParcelNoticeResultServer();
        $SmsLog = new \Home\Server\SmsLogServer();
        //根据短信log判断12小时只能发三次
        $queryNum = $SmsLog->countSmsLog( $parcelId );

        if ($queryNum >= '3' && $spareNum ==0 /* 回调重发不检测*/){
            $this->auto_error('12小时内发送次数超过三次');
        }

        $data = $parcelDetail->getParcelDetails($parcelId);

        //判断短信余量
        if ($data['fBuyCount'] == 0){
            $this->auto_error('短信余量不足');
        }
        //根据用户id查询短信模板没有默认模板则调用系统模板
        if ($data['fTemplateId'] == 0){
            $id='';
            $arr = D('Home/SmsTemplate')->getemplate($id);
        }else{
            //关联站点下的短信模板
            $arr = D('Home/SmsTemplate')->getemplate($data['fTemplateId']);
        }
        //判断短信模板获取失败状态为D  12-22
        if(empty($arr['f_template_content'])){
            $this->output(0);
        }
        //替换短信模  板
        $autograph = C('AUTOGRAPH');
        $list = str_replace('{companyName}',$data['companyName'],$arr['f_template_content']);
        $list = str_replace(array('{mailNo}','{address}','{extractCode}','{openTime}','{mobile}','{frameCode}'),array(substr_replace($data['mailNo'],'**', 0,-4),$data['address'],$data['extractCode'],$data['openTime'],$data['userPhone'],$data['fFrameCode'].$data['fNum']),$list);
        //如果短信模板替换失败则短信发送状态为2，错误码为E  12-22
        if(empty($list)){
            $this->output(0);
        }
        $autograph .= $list;
        $result = send_sms($data['mobile'],$autograph,$spareNum);

        $patterns = "/\d+/";
        preg_match_all($patterns,$result,$msg);
        //计算短信条数
        if (mb_strlen($autograph,'utf-8')<=70){
            $smsnum = '1';
        }else{
            //超过70字，按64字每条计算
            $smsnum = ceil(mb_strlen($autograph,'utf-8')/64);
        }

        if($msg['0']['1'] == '0'){
            //更新短信余额
            D('Home/User')->updateSms($data['stationId'],$smsnum);
            //写入短信log
            $SmsLog->addLog( $data , $msg['0']['2'] , $msg['0']['1'] , $autograph , $smsnum , '1' );
            //更新短信内容
            $parcelNotice->updateSendNotice($data['packageCode'],$autograph,null,null);
            $this->output(1);
        }else{
            //写入短信log
//            $SmsLog->addLog( $data , $msg['0']['2'] , $msg['0']['1'] , $autograph , '0' , '1' );
            $this->output(0);
        }
	}

	/**
	 * @desc 队列短信发送方法
	 * @author quyang
     * @again quyang 2017-1-17
	 * @update 2016-12-17
	 */
	public function queueSendSms($parcelId ,$smsType = 0){
//	    $parcel = explode(',',$parcelId);
	    //获取替换信息
	   
	    foreach ($parcelId as $value){

            $parcelDetail = new \Home\Server\QueryParcelService();
            $parcelNotice = new \Home\Server\ParcelNoticeResultServer();
            $SmsLog = new \Home\Server\SmsLogServer();

            $data = $parcelDetail->getParcelDetails($value);

            $map = array('f_parcel_id'=>$data['packageCode']);
            $where = array('f_parcel_status'=>'5','f_last_time'=>date('YmdHis'),'f_last_timestamp'=>time());
            
    	    //判断短信余量
    	    if ($data['fBuyCount'] == 0){

                $parcelNotice->updateSendNotice($data['packageCode'],null,'2','B');
                //提交失败包裹状态改为5通知失败
                $parcelDetail->updateParcel($map,$where);
                continue;
            }
    	    //根据用户id查询短信模板没有默认模板则调用系统模板
    	    if ($data['fTemplateId'] == 0){
    	        $id='';
    	        $arr = D('Home/SmsTemplate')->getemplate($id);
    	    }else{
    	        //关联站点下的短信模板
    	        $arr = D('Home/SmsTemplate')->getemplate($data['fTemplateId']);
    	    }
    	    
    	    //判断短信模板获取失败状态为D  12-22
    	    if(empty($arr['f_template_content'])){
                $parcelNotice->updateSendNotice($data['packageCode'],null,'2','D');
                //提交失败包裹状态改为5通知失败
                $parcelDetail->updateParcel($map,$where);
                continue;
            }
            
    	    //替换短信模板
    	    $autograph = C('AUTOGRAPH');
            $list = str_replace('{companyName}',$data['companyName'],$arr['f_template_content']);
            $list = str_replace(array('{mailNo}','{address}','{extractCode}','{openTime}','{mobile}','{frameCode}'),array(substr_replace($data['mailNo'],'**', 0,-4),$data['address'],$data['extractCode'],$data['openTime'],$data['userPhone'],$data['fFrameCode'].$data['fNum']),$list);
           
            //如果短信模板替换失败则短信发送状态为2，错误码为E  12-22
            if(empty($list)){
                $parcelNotice->updateSendNotice($data['packageCode'],null,'2','E');
                //提交失败包裹状态改为5通知失败
                $parcelDetail->updateParcel($map,$where);
                continue;
            }
            $autograph .= $list;
            
    	    $result = send_sms($data['mobile'],$autograph);
    	    $patterns = "/\d+/";
    	    preg_match_all($patterns,$result,$msg);
    	    //计算短信条数
    	    if (mb_strlen($autograph,'utf-8')<=70){
    	        $smsnum = '1';
            }else{
                //超过70字，按64字每条计算
                $smsnum = ceil(mb_strlen($autograph,'utf-8')/64);
            }

     	    if($msg['0']['1'] == '0'){
                //更新短信内容、发送状态
                $parcelNotice->updateSendNotice($data['packageCode'],$autograph,'3','0');
                //更新短信余额
                D('Home/User')->updateSms($data['stationId'],$smsnum);
                //写入短信log
                $SmsLog->addLog( $data , $msg['0']['2'] , $msg['0']['1'] , $autograph , $smsnum , $smsType );
     	    }else{
                //短信发送失败改状态为发送失败
                $parcelNotice->updateSendNotice($data['packageCode'],$autograph,'2','C');
                //写入短信log
//                $SmsLog->addLog( $data , $msg['0']['2'] , $msg['0']['1'] , $autograph , '0' , '0' );
                //提交失败包裹状态改为5通知失败
                $parcelDetail->updateParcel($map,$where);
            }
	    }
	}

	/**
	 * @desc:  根据手机号返回站点id
	 * @Author: zhoutai
     * @again quyang 2017-1-17
     * @update: 16/12/16
	 **/
	public function getStationIdWithMobile(){
        $data = I('param.');
        if (empty($data['mobile'])) $this->param_error(0);
        $stationInfo = D('tp_user')->where(array('f_user_mobile' => $data['mobile'], 'f_user_type_id' => 0))->field('
            f_user_id as stationId,
            f_user_type_id as userType
        ')->find();
        if (empty($stationInfo)) $this->output(0);
        ob_clean();
        $this->output(1, $stationInfo);
    }


	/**
	 * @desc:  根据站点id 返回地址信息
	 * @Author: zhoutai
     * @again quyang 2017-1-21
	 * @update: 16/12/16
	 **/

	public function getAddressWithStationId(){
        $data = I('param.');
        $stationId = htmlspecialchars_decode($data['stationId']);
        if (empty($stationId)) $this->param_error(0);
        $stationIds = json_decode($stationId);
        $result = array();
        foreach ($stationIds as $k => $v){
            $resultData = D('tp_user d')
                ->where(array('d.f_user_id' => $v, 'd.f_user_status' => '1'))->field('
               d.f_province_id as provinceId,
               d.f_city_id as cityId,
               d.f_area_id as areaId,
               d.f_province_name as provinceName,
               d.f_city_name as cityName,
               d.f_area_name as areaName,
               d.f_address as address,
               d.f_user_id as stationId,
               d.f_station_name as stationName
        ')->find();

            if( $resultData )
            {
                $result[] = $resultData;
            }
        }
        if (empty($result)) $this->output(0);
        ob_clean();
        $this->output(1, $result);
    }

    /**
     * @desc 通知失败重发短信(重新加入队列)
     * @quthor quyang
     * @again quyang 2017-1-21
     * @update 2016-12-24
     */
    public function reSendPacket(){
        $parcelId = I('param.packetIds');
        $parcel = explode(',',$parcelId);
        $update = array(
            'f_weixin_notice'            => 0,
            'f_weixin_num'               => 0,
            'f_sms_notice'               => 0,
            'f_sms_num'                  => 0,
            'f_create_time'              => date('YmdHis', time()),
            'f_create_time_stamp'        => time(),
            'f_weixin_send_time'         =>'',
            'f_weixin_send_time_stamp'   =>0,
            'f_weixin_success_time'      =>'',
            'f_weixin_success_time_stamp'=>0,
            'f_sms_success_time'         =>'',
            'f_sms_success_time_stamp'   =>0,
            'f_sms_error'                =>'',
            'f_sms_send_time'            =>'',
            'f_sms_send_time_stamp'      =>0
        );
        foreach ($parcel as $v){

            $noticeServer = new \Home\Server\ParcelNoticeResultServer();
            $arr = $noticeServer->queryParcelNotice( $v );

            if ( $arr == false ){
                //获取包裹详情
                $parcelDetail = new \Home\Server\QueryParcelService();

                $list = $parcelDetail->reSendParcelDetails($v);

                //包裹不存在执行下一个
                if ( $list == false ){
                    continue;
                }

                $data['f_parcel_id'] = $list['f_parcel_id'];
                $data['f_station_id'] = $list['f_station_id'];
                $data['f_recipient_mobile'] = $list['f_recipient_mobile'];
                $data['f_recipient_name'] = $list['f_recipient_name'] ? $list['f_recipient_name'] : '';
                $data['f_mail_no'] = $list['f_mail_no'];
                $data['f_take_code'] = $list['f_take_code'];
                $data['f_company_name'] = $list['f_company_name'];
                $data['f_address'] = $list['f_address'] ? $list['f_address'] : '';
                $data['f_qtime'] = $list['f_store_time'] ? $list['f_store_time'] : '';
                $data['f_tel'] = $list['f_user_mobile'] ? $list['f_user_mobile'] : '';
                $data['f_station_name'] = $list['f_station_name'] ? $list['f_station_name'] : '';
                $data['f_create_time'] = date('YmdHis');

                $noticeServer->addParcelQueue( $data );

            }else{

                $noticeServer->updateParcelNotice( $v , $update );

            }
        }
        $this->output(1);
    }

    /**
     * @desc app保存信息重发通知
     * @author quyang
     * @again quyang 2017-1-21
     * @param parcelId包裹Id mobile手机号 identifier是否重发短信 stationId站点id comment备注内容
     * @update 2016-12-24
     */
    public function preservationMobile(){
        $param = I('param.');
        if (empty($param['parcelId']) || empty($param['mobile'])  || $param['identifier'] == '' || $param['stationId'] == ''){
            $this->param_error();
        }

        $parcelDetail = new \Home\Server\QueryParcelService();

        //查询队列表
        $Notice = new \Home\Server\ParcelNoticeResultServer();
        $data = $Notice->queryParcelNotice($param['parcelId']);

        //判断手机号、姓名是否更改
        if ($data['f_recipient_mobile'] != $param['mobile'] || $data['f_recipient_name'] != $param['name']){
            //更新手机号、姓名、备注
            !empty($param['comment']) && $map['f_comment'] = $param['comment'];
            $map['f_recipient_mobile'] = $param['mobile'];
            $map['f_recipient_name'] = $param['name'];
            $map['f_last_time'] = date('YmdHis');
            $map['f_last_timestamp'] = time();
            $where = array('f_parcel_id'=>$param['parcelId'],'f_station_id'=>$param['stationId']);

            //判断是否重发通知，重发则生成新的提货码  2017-03-20
            $new_tack_code = '';
            if ( $param['identifier'] == '0' ){
                $new_tack_code = $data['f_take_code'];
            }

            $tackCode = $parcelDetail->updateParcelTackcode( $param['stationId'] , $where , $map , $new_tack_code );
            if ($tackCode == false){
                $this->output(0);
            }

            //判断队列表是否  存在则更新相关信息
            if (!empty($data)){
                $where = array(
                    'f_recipient_mobile'=>$param['mobile'],
                    'f_recipient_name'=>$param['name'],
                    'f_take_code'=>$tackCode
                );
                //更新包裹状态
                $Notice->updateParcelNotice( $param['parcelId'] , $where );
            }else{
                $this->output(true);
            }
        }elseif (!empty($param['comment'])){
            //单独更新备注信息
            $map['f_comment'] = $param['comment'];
            $map['f_last_time'] = date('YmdHis');
            $map['f_last_timestamp'] = time();
            $where = array('f_parcel_id'=>$param['parcelId'],'f_station_id'=>$param['stationId']);
            //更新包裹表
            $parcelDetail = new \Home\Server\QueryParcelService();
            $parcelDetail->updateParcel($where,$map);
        }

        if ($param['identifier'] == '0'){
            $this->output(1);
        }

        //0、2、5状态包裹以外的包裹不允许重发通知
        $parcelInfo = $parcelDetail->queryParcelStatus( "`f_parcel_id`={$param['parcelId']}" );
        if ( empty($parcelInfo) || ( $parcelInfo['f_parcel_status'] !='0' && $parcelInfo['f_parcel_status'] != '2' && $parcelInfo['f_parcel_status'] != '5' ) )
        {
            $this->auto_error( '该状态包裹不允许重发通知' );
        }

        //如果值为1就保存并发送通知
        $this->repeatNotice($data);
    }

    /**
     * Function repeatSms
     *
     * Author: quyang
     * Date: 2017-2-8 18:02
     * Info: 批量重发通知
     */
    public function repeatSms(){

        $param = I('param.');

        if (empty($param['parcelId']) || empty($param['stationId'])){

            $this->param_error();

        }

        //判断接到的数据最后一位
        if ( substr($param['parcelId'] , -1 , 1) == ',' ){
            $parcelId = substr($param['parcelId'],0,strlen($param['parcelId'])-1);
        }else{
            $parcelId = $param['parcelId'];
        }

        $Notice = new \Home\Server\ParcelNoticeResultServer();
        $data = $Notice->sendParcelNotice( $parcelId , $param['stationId'] );

        if ($data == false){
            $this->output(false);
        }

        foreach ($data as $v){
            $this->repeatNotice( $v );
        }

    }

    /**
     * @desc 已到站重发通知
     * @author quyang
     * @again quyang 2017-1-18
     * @update 2016-12-24
     */
    public function repeatNotice($data){
        //根据站点id，查询是否是专业版，若为专业版则使用包裹编号作为提货码
        $packageTable = 'tp_parcel_info_'.preg_replace('/\D/s', '', $data['table_name']);
        $package_num = get_special_pkg($data['f_station_id'],$data['f_parcel_id'],$packageTable);
        if($package_num){
            $data['f_take_code'] = $package_num.'(包裹编号)';
        }

		//根据上次发送状况获取本次重发类型
        if ($data['f_weixin_notice'] == '1'){
            $weixin = $data;
        }
        if ($data['f_sms_notice'] == '1'){
            $sms = $data['f_parcel_id'];
        }

        if ($data['f_weixin_notice'] != '1' && $data['f_sms_notice'] != '1'){
            $where = array(
                'f_weixin_notice'            => 0,
                'f_weixin_num'               => 0,
                'f_sms_notice'               => 0,
                'f_sms_num'                  => 0,
                'f_create_time'              => date('YmdHis', time()),
                'f_create_time_stamp'        => time(),
                'f_weixin_send_time'         =>'',
                'f_weixin_send_time_stamp'   =>0,
                'f_weixin_success_time'      =>'',
                'f_weixin_success_time_stamp'=>0,
                'f_sms_success_time'         =>'',
                'f_sms_success_time_stamp'   =>0,
                'f_sms_error'                =>'',
                'f_sms_send_time'            =>'',
                'f_sms_send_time_stamp'      =>0
            );
			
			if ($data == false){
				$this->output(false);
			}

            $Notice = new \Home\Server\ParcelNoticeResultServer();
            $Notice->updateParcelNotice( $data['f_parcel_id'] , $where , $data['table_name'] );
            $this->output(1);
        }


        //确定上次微信推送成功重发微信
        if (!empty($weixin)){
            $weinNotice = $this->noticeInfo($weixin);
            if (!empty($weinNotice)){
                $sms = $weinNotice;
            }
        }

        //确定上次短信推送成功重发短信
        if (!empty($sms)){
            $this->repeatSendSms($sms);
        }
    }

    /**
     * @desc 包裹微信通知
     * @author quayng
     * @update 2016-12-24
     */
    public function noticeInfo($result) {
        //发微信并记录发送微信次数，根据返回值修改微信通知状态或成功发送时间
        $data = array(
            'mobile' => $result['f_recipient_mobile'], //收件人手机号码
            'name' => $result['f_recipient_name'], //收件人姓名
            'mailno' => $result['f_mail_no'], //运单号
            'code' => $result['f_take_code'], //提货码
            'company' => $result['f_company_name'], //快递公司名称
            'address' => $result['f_address'], //站点地址
            'qtime' => $result['f_qtime'], //站点营业时间
            'remark' => '感谢您的使用',
            'tel' => $result['f_tel'], //站点的联系电话
			'type' => $result['f_speed_status']		//包裹类型 0 普通 1 极速

        );
        $return_weixin = send_weixin($data);
        $arr = json_decode($return_weixin, true);
        if ($arr['status'] == '1'){
            return $result['f_parcel_id'];
        }else{
            $this->output(1);
        }
    }

    /**
     * @desc: 对外发送短信方法
     * @Author: delayhsiao
     * @update: 2016-12-27
     **/
    public function openSendSms(){
    	$data = I('param.');
    	if(empty($data['content']) || empty($data['mobile'])){
    		$this->param_error();
		}
		$content = C('AUTOGRAPH').$data['content'];
		$result = send_sms($data['mobile'],$content);
		$errCode = explode("\n",$result);
		$err = explode(',',$errCode[0]);
		if($err[1] == 0){
			$this->output(true);
		}else{
			$this->output(false);
		}
	}

    /**
     * @desc: 找回密码  发送手机短信验证码
     * @Author: quyang
     * @update: 2017-02-06
     **/
    public function sendVerify(){
        $data = I('param.');
        if(empty($data['mobile'])){
            $this->output(false);
        }
        $verifyCode = rand(100000,999999);
        $smsTpl = C('AUTOGRAPH')."您的验证码为：".$verifyCode."，如有任何疑问可联系在线客服或拨打025-84185858";
        $result = send_sms($data['mobile'],$smsTpl);
        $errCode = explode("\n",$result);
        $err = explode(',',$errCode[0]);
        if($err[1] == 0){
            $return = array('verifyCode' => (string)$verifyCode);
            $this->output(true,$return);
        }else{
            $this->output(false);
        }
    }

	public function demoTcp(){
		$return = array(
			'status' => 0,
			'msg'	=> 'ok'
		);
		$this->output(true,$return);

	}

	public function getredis(){
		$server = new QPS();
		$UserInfo = $server->getUserInfo(I('sid'));
		$return = array(
			'status' => 0,
			'msg'	=> $UserInfo
		);
		$this->output(true,$return);
	}

    /**
     * @desc:  根据站点id返回站点name
     * @Author: luwushuang
     * @time: 2017-7-20
     * @update:
     **/
    public function getStationIdInfo(){
        $data = I('param.');
        if (empty($data['stationId'])) $this->param_error(0);
        $stationInfo = D('tp_user')->where(array('f_user_id' => $data['stationId'], 'f_user_type_id' => 0))->field('
            f_user_id as stationId,
            f_station_name as stationName
        ')->find();
        if (empty($stationInfo)) $this->output(0);
        ob_clean();
        $this->output(1, $stationInfo);
    }
}